<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <p>获取xml</p>
    <script>
        //创建xhr
        var xhr=new XMLHttpRequest()
        xhr.open('get','02.xml',true)
        xhr.send()
        xhr.onreadystatechange=function(){
            if(xhr.readyState==4){
                if(shr.status==200){
                    console.log(xhr.responseXML)
                    var data=xhr.responseXML
                    let bookxml=data.getElementsByTagName('bookstore')[0]
                    let books=bookxml.getElementsByTagName('book')
                    console.log(books)
                    //遍历
                    for (var i;i<books.length;i++){
                        console.log(books[i])
                        console.log(books[i].getElementsByTagName('name')[0].innerHTML)
                    }
                }
            }
        }

    </script>
</body>
</html>